package pers.wchen.mybatis.generate.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @作者： wchen
 * @描述： 文件操作工具
 * @创建时间： 2018/2/4 16:37
 * @版本：Copyright 2018, Inc. All Rights Reserved.
 * @修改信息：
 */
public class FileUtil {

    private static FileReader fileReader = null;
    private static BufferedReader bufferedReader = null;

    private static FileWriter fileWriter = null;
    private static BufferedWriter bufferedWriter = null;

    /**
     *
     * @描述： 读取文件
     * @名称： readTemp
     * @入参： [filePath]
     * @出参： java.lang.StringBuffer
     * @修改信息： 创建：wchen 2018/2/4
     */
    public static StringBuffer readTemp(String filePath) {
        StringBuffer sbf = new StringBuffer();
        try {
            fileReader = new FileReader(filePath);
            bufferedReader = new BufferedReader(fileReader);
            String line;
            while ((line = bufferedReader.readLine()) != null){
    //            System.out.println(line);
                sbf.append(line).append("\n");
            }
        }catch (IOException e){
            System.out.println(filePath + "文件不存在！读取失败！");
//            e.printStackTrace();
        }finally {
            try {
                if(bufferedReader != null) {
                    bufferedReader.close();
                }
                if(fileReader != null) {
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sbf;
    }



    /**
     *
     * @描述： 内容写入文件
     * @名称： writeFile
     * @入参： [filePath, content]
     * @出参： void
     * @修改信息： 创建：wchen 2018/2/4
     */
    public static void writeFile(String filePath, String content) {

        File file = new File(filePath);
        try {
            fileWriter = new FileWriter(file);
            bufferedWriter = new BufferedWriter(fileWriter);

            bufferedWriter.write(content);
            bufferedWriter.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(bufferedWriter != null) {
                    bufferedWriter.close();
                }
                if(fileWriter != null) {
                    fileWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
//        System.out.println("生成成功! 路径:" + file.getPath());
    }

    /**
     *  @描述: 获取指定目录下的所有文件
     *  @参数： [directoryPath, regularPath]
     *  @返回值： java.util.List<java.lang.String>
     *  @创建人: wchen
     *  @创建时间: 2018/2/9
     *  @修改人和其它信息:
     */
    public static List<String> getAllFile(String directoryPath, String regularPath) {

        List<String> list = new ArrayList<String>();
        File baseFile = new File(directoryPath);
        if (baseFile.isFile() || !baseFile.exists()) {
            return list;
        }
        File[] files = baseFile.listFiles();
//        Pattern pattern = Pattern.compile(regularPath);
        for (File file : files) {
            String fileName = file.getName();
            System.out.println("fileName: " + fileName);
            //文件夹略过
            if (file.isDirectory()){
                continue;
            }
            if(fileName.contains(regularPath)){
                list.add(file.getAbsolutePath());
            }
        }
        return list;
    }


}
